package cn.tom.controller.fore;

import cn.tom.tools.TokenAndUser;
import cn.tom.tools.XResp;
import cn.tom.entity.Comment;
import cn.tom.tools.XJson;
import cn.tom.entity.XUser;
import cn.tom.service.CommentService;
import cn.tom.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/fore/rate")
public class foreComController {
    @Resource
    private CommentService commentService;
    @GetMapping("/findByUid/{pagenum}/{lines}")
    public XJson findByUid(@PathVariable("pagenum")Integer pagenum,
                           @PathVariable("lines")Integer lines)
    {
        if(pagenum < 1) pagenum = 1;
        if(lines < 2) lines = 2;
        XUser xUser = (XUser) TokenAndUser.findOne().getData();
        List<Comment> coms = commentService.findByid(null,xUser.getId() , pagenum, lines);
        int total          = commentService.total(null,xUser.getId());
        HashMap<String,Object> map = new HashMap<>();
        map.put("comments"   , coms  );
        map.put("total"      , total );
        return XResp.pack(200,"",map);
    }
}
